//
//  YDOAWoringLunchManVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/5/26.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit
import HandyJSON
import Toaster
class YDOAWoringLunchManVC: UIViewController {

    @IBOutlet weak var myTableView: UITableView!
    
    lazy var dataSource : NSMutableArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "今日勾餐列表"
        myTableView.backgroundColor = UIColor.clear
        myTableView.tableHeaderView?.backgroundColor = UIColor.clear
        myTableView.tableFooterView?.backgroundColor = UIColor.clear
        myTableView.backgroundView?.backgroundColor = UIColor.clear
        self.automaticallyAdjustsScrollViewInsets = false
        self.edgesForExtendedLayout = .init(rawValue: 0)

        setupUI()
        loadData();
    }

//    获取今日已勾餐的列表
    func loadData(){
        
        let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
        let token = "token" //userinfo["token"] as! String
        let id = userinfo["id"] as? Int
        let urlStr = String(format:GET_WORKING_LUNCH_LIST,token,id!)
        YMLog.log(item: urlStr)
        YMNetworkManager().getRequest(urlString: urlStr, params: nil, success: { (responser) in
            if responser["status"]?.int8Value == 1{
               let dataArr = responser["data"] as! NSArray
                if dataArr.count == 0{
                    Toast(text: "今日暂无勾餐员工", delay: 0, duration: 1.5).show()
                }
                for dict  in dataArr{
                    guard let model =  JSONDeserializer<YDOAWorkingLunchModel>.deserializeFrom(dict: dict as? NSDictionary) else {return}
                    self.dataSource.add(model)
                }
                self.myTableView.reloadData()
             }
        }) { (error) in
            
        }
    }
    

    private func setupUI(){
        navigationController?.navigationBar.isHidden = false
        
     
        
//        myTableView.register(UINib(nibName: "YDLunchCell", bundle: nil), forCellReuseIdentifier: "YDLunchCell")
      myTableView.register(UINib(nibName: "YDOAWorkingLunchCell", bundle: nil), forCellReuseIdentifier: "YDOAWorkingLunchCell")
        myTableView.separatorStyle = .none
        myTableView.reloadData()
    }
    
    
 
    
    //发布工作餐
    @IBAction func senderBtnClick(_ sender: Any) {
        let vc =  YDAddLunchVC()
        vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
        vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
        
        self.present(vc, animated: true, completion: {
            
        })

        
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}


extension YDOAWoringLunchManVC : UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if section == 0 {
            return 20
        }
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "YDOAWorkingLunchCell") as? YDOAWorkingLunchCell  else {
            return YDOAWorkingLunchCell()
        }
        
        cell.contentView.backgroundColor = UIColor.clear
        cell.selectionStyle = .none
        let model = self.dataSource[indexPath.section] as! YDOAWorkingLunchModel
        cell.model = model
        
        //点击A或B套餐时触发事件
//        cell.callbackWithType = { (type : Type , title : String?) in
//            let vc =  LoginPresentVC()
//            if type == Type.A {
//                vc.msg = "你确定选择A套餐吗？\n \(title!)"
//
//            }else{
//                vc.msg = "你确定选择B套餐吗？\n \(title!)"
//            }
//            vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
//            vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
//
//            self.present(vc, animated: true, completion: {
//
//            })
//        }
        return cell
    }
    
    
    
    
}
